home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / toxetris / tetris.bas < prev    next >
BASIC Source File  |  1999-09-30  |  16KB  |  333 lines

  1. Attribute VB_Name = "Module1"
  2. Option Explicit
  3. 'Only one API declaration
  4. 'from The Visual Basic 5.0 API declarations document
  5. 'Win32API.txt
  6. Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  7. Public Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
  8. Public Const SRCPAINT = &HEE0086        ' (DWORD) dest = source OR dest
  9. Public Const SRCAND = &H8800C6  ' (DWORD) dest = source AND dest
  10. Public Const SRCINVERT = &H660046       ' (DWORD) dest = source XOR dest
  11. Public Const SRCERASE = &H440328        ' (DWORD) dest = source AND (NOT dest )
  12. Public Const NOTSRCCOPY = &H330008      ' (DWORD) dest = (NOT source)
  13. Public Const NOTSRCERASE = &H1100A6     ' (DWORD) dest = (NOT src) AND (NOT dest)
  14.  
  15. Public Type ONE_PIECE
  16.    Width As Long
  17.    Height As Long
  18.    OnBmp_x As Long 'x pos of the piece on the Pieces.BMP file
  19.    OnBmp_y As Long 'y pos
  20.    MaskOnBmp_x As Long 'x pos of the mask on the Pieces.BMP file
  21.    MaskOnBmp_y As Long 'y pos
  22.    Creating_x As Long 'Center the piece on release
  23.    Creating_Piece_No As Long 'Allways release the good one.
  24.    Next_Piece_Pos_x As Long 'Center the piece on the next piece window
  25.    Next_Piece_Pos_y As Long 'Same
  26.    After_Turn_Piece_No As Long 'Which one will be the next piece after turn
  27.    After_Turn_dx As Long 'if it will turn, how much will
  28.    After_Turn_dy As Long 'be the x,y differences on turn
  29.    Number_Of_Positions_To_Check_Left As Long 'Number of positions To check on the left, right and down
  30.    Number_Of_Positions_To_Check_Right As Long
  31.    Number_Of_Positions_To_Check_Down As Long
  32.    Check_These_Positions_Left_x(9) As Long 'X and Y coordinates of the Positions to check if empty or not
  33.    Check_These_Positions_Left_y(9) As Long
  34.    Check_These_Positions_Right_x(9) As Long
  35.    Check_These_Positions_Right_y(9) As Long
  36.    Check_These_Positions_Down_x(9) As Long
  37.    Check_These_Positions_Down_y(9) As Long
  38.    Number_Of_Squares As Long 'It is allways equal to 4
  39.    Piece_Is_This_x(4) As Long
  40.    Piece_Is_This_y(4) As Long
  41. End Type
  42.  
  43. Type PIECE_PERMISSIONS
  44.    Left(1 To 18, 0 To 11) As Boolean
  45.    Right(1 To 18, 0 To 11) As Boolean
  46.    Vertical(1 To 18, 0 To 11) As Boolean
  47.    Left2(1 To 18, 0 To 11) As Boolean
  48.    Right2(1 To 18, 0 To 11) As Boolean
  49.    Vertical2(1 To 18, 0 To 11) As Boolean
  50. End Type
  51.  
  52. Public Piece(19) As ONE_PIECE
  53. Public Piece_No_Permission As PIECE_PERMISSIONS
  54. Public Score As Long
  55. Public Vertical_Stop_Time As Single
  56. Public Vx As Single
  57. Public Vy As Single
  58. Public Vy_Level As Single
  59. Public Level As Long
  60. Public x As Single
  61. Public y As Single
  62. Public Prv_x As Single
  63. Public Prv_y As Single
  64. Public Piece_No As Long
  65. Public Prv_Piece_No As Long
  66. Public Next_Piece_No As Long
  67. Public Position_Empty(1 To 18, 0 To 11) As Boolean
  68. Public Copied_To_BMP As Boolean
  69. Public Game_Is_Started As Boolean
  70. Public Game_Is_About_To_End As Boolean
  71. Public Key_Up As Boolean
  72. Public Vertical_Stop_Status As Boolean
  73. Public Left_Stop_Status As Boolean
  74. Public Right_Stop_Status As Boolean
  75. Public Prv_Vertical_Stop_Status As Boolean
  76. Public Prv_Left_Stop_Status As Boolean
  77. Public Prv_Right_Stop_Status As Boolean
  78. Public Right_Move_Requested As Boolean
  79. Public Left_Move_Requested As Boolean
  80. Public Piece_Stopped As Boolean
  81. Sub Draw_The_Piece(DTP_Prv_Piece_No As Long, DTP_Piece_No As Long, DTP_x As Long, DTP_y As Long, DTP_Px As Long, DTP_Py As Long)
  82.    'First, copy the previous background back
  83.    'Then copy the new position content to a blank area on picture box BMP
  84.    'Draw the mask
  85.    'and draw the piece to its new position.
  86.       If Copied_To_BMP = True Then BitBlt Form1.Picture1.hDC, DTP_Px, DTP_Py, Piece(DTP_Prv_Piece_No).Width, Piece(DTP_Prv_Piece_No).Height, Form1.BMP.hDC, 408, 240, SRCCOPY
  87.       BitBlt Form1.BMP.hDC, 408, 240, Piece(DTP_Piece_No).Width, Piece(DTP_Piece_No).Height, Form1.Picture1.hDC, DTP_x, DTP_y, SRCCOPY
  88.       Copied_To_BMP = True
  89.       BitBlt Form1.Picture1.hDC, DTP_x, DTP_y, Piece(DTP_Piece_No).Width, Piece(DTP_Piece_No).Height, Form1.BMP.hDC, Piece(DTP_Piece_No).MaskOnBmp_x, Piece(DTP_Piece_No).MaskOnBmp_y, SRCAND
  90.       BitBlt Form1.Picture1.hDC, DTP_x, DTP_y, Piece(DTP_Piece_No).Width, Piece(DTP_Piece_No).Height, Form1.BMP.hDC, Piece(DTP_Piece_No).OnBmp_x, Piece(DTP_Piece_No).OnBmp_y, SRCPAINT
  91.       Form1.Picture1.Refresh
  92. End Sub
  93.  
  94. Public Function Get_A_Piece() As Long
  95.    Get_A_Piece = 1 + Int(Rnd * 19)
  96. End Function
  97.  
  98. Sub Check_The_Permissions(CTP_Piece_No As Long, CTP_x As Single, CTP_y As Single, CTP_Vx As Single, CTP_Vy As Single)
  99. Dim CTP_x_Pos
  100. Dim CTP_y_Pos
  101. CTP_x_Pos = Int(CTP_x / 24) + 1
  102. CTP_y_Pos = Int((CTP_y + CTP_Vy) / 24) + 1
  103. Vertical_Stop_Status = False
  104. Right_Stop_Status = False
  105. Left_Stop_Status = False
  106. If Int((CTP_y + CTP_Vy) / 24) > Int(CTP_y / 24) Or Int(CTP_y) Mod 24 = 0 Then
  107.    Vertical_Stop_Status = False
  108.    If Int(CTP_x) Mod 24 = 0 Then
  109.       If Piece_No_Permission.Vertical(CTP_y_Pos, CTP_x_Pos) = True Then
  110.          y = Int((CTP_y + CTP_Vy) / 24) * 24: Vy = 0: Vertical_Stop_Status = True
  111.          If Vertical_Stop_Time = 0 Then Vertical_Stop_Time = Timer
  112.          If Vertical_Stop_Time > 0 And Timer - Vertical_Stop_Time > 0.5 Then Stop_The_Piece CTP_Piece_No, CTP_x, CTP_y: Exit Sub
  113.       End If
  114.    Else
  115.       If Piece_No_Permission.Vertical(CTP_y_Pos, CTP_x_Pos) = True Or Piece_No_Permission.Vertical2(CTP_y_Pos, CTP_x_Pos) = True Then
  116.          y = Int((CTP_y + CTP_Vy) / 24) * 24: Vy = 0: Vertical_Stop_Status = True
  117.          If Vertical_Stop_Time > 0 And Timer - Vertical_Stop_Time > 0.5 Then Stop_The_Piece CTP_Piece_No, CTP_x, CTP_y: Exit Sub
  118.          If Vertical_Stop_Time = 0 Then Vertical_Stop_Time = Timer
  119.       End If
  120.    End If
  121.    If Vertical_Stop_Status = False And Vy = 0 Then Vy = Vy_Level
  122. End If
  123. If Int((CTP_x + CTP_Vx) / 24) < Int(CTP_x / 24) Or Int(CTP_x) Mod 24 = 0 Then
  124.    Left_Stop_Status = False
  125.    If Int(CTP_y) Mod 24 = 0 Then
  126.       If Piece_No_Permission.Left(CTP_y_Pos, CTP_x_Pos - 1) = True Then
  127.          x = Int(CTP_x / 24) * 24: Vx = 0: Left_Stop_Status = True
  128.       End If
  129.    Else
  130.       If Piece_No_Permission.Left(CTP_y_Pos, CTP_x_Pos - 1) = True Or Piece_No_Permission.Left2(CTP_y_Pos, CTP_x_Pos - 1) = True Then
  131.          x = Int(CTP_x / 24) * 24: Vx = 0: Left_Stop_Status = True
  132.       End If
  133.    End If
  134. End If
  135. If Int((CTP_x + CTP_Vx) / 24) > Int(CTP_x / 24) Or Int(CTP_x) Mod 24 = 0 Then
  136.    Right_Stop_Status = False
  137.    If Int(y) Mod 24 = 0 Then
  138.       If Piece_No_Permission.Right(CTP_y_Pos, CTP_x_Pos) = True Then
  139.          x = Int(CTP_x / 24) * 24: Vx = 0: Right_Stop_Status = True
  140.       End If
  141.    Else
  142.       If Piece_No_Permission.Right(CTP_y_Pos, CTP_x_Pos) = True Or Piece_No_Permission.Right2(CTP_y_Pos, CTP_x_Pos) = True Then
  143.          x = Int(CTP_x / 24) * 24: Vx = 0: Right_Stop_Status = True
  144.       End If
  145.    End If
  146. End If
  147. If Prv_Right_Stop_Status = True And Right_Stop_Status = False And Right_Move_Requested = True Then
  148.    x = x + 1: Vx = 0.1: Exit Sub
  149. End If
  150. If Vertical_Stop_Status = False Then Vertical_Stop_Time = 0
  151. If Prv_Vertical_Stop_Status = True And Vertical_Stop_Status = False Then
  152.    y = y + 1: Vy = Vy_Level: Exit Sub
  153. End If
  154. If Right_Move_Requested = True And Left_Stop_Status = True And Right_Stop_Status = False Then Vx = 0.1: x = x + 1
  155. If Left_Move_Requested = True And Right_Stop_Status = True And Left_Stop_Status = False Then Vx = -0.1: x = x - 1
  156. End Sub
  157.  
  158. Sub Create_Permission_Database(CPD_Piece_no As Long)
  159. Dim CPD_1 As Long
  160. Dim CPD_2 As Long
  161. Dim CPD_3 As Long
  162. Dim CPD_4 As Long
  163. Dim CPD_5 As Long
  164. For CPD_1 = 1 To 18
  165.    For CPD_2 = 0 To 11
  166.       Piece_No_Permission.Vertical(CPD_1, CPD_2) = False
  167.       Piece_No_Permission.Vertical2(CPD_1, CPD_2) = False
  168.       Piece_No_Permission.Left(CPD_1, CPD_2) = False
  169.       Piece_No_Permission.Left2(CPD_1, CPD_2) = False
  170.       Piece_No_Permission.Right(CPD_1, CPD_2) = False
  171.       Piece_No_Permission.Right2(CPD_1, CPD_2) = False
  172.       If Position_Empty(CPD_1, CPD_2) = False Then
  173.          For CPD_3 = 1 To Piece(CPD_Piece_no).Nu